import type { HttpRequestInterceptor } from '@/config/httpClient/HttpInterceptor.ts'
import type { AxiosInterceptorOptions, AxiosRequestConfig, ParamsSerializerOptions } from 'axios'
import { stringify } from 'qs'

/**
 * 查询参数编码拦截器
 */
export class QueryParameterEncodingInterceptor implements HttpRequestInterceptor {

    options: AxiosInterceptorOptions = {
        synchronous: true,
        runWhen(config) {
            // 只在有查询参数时运行
            return !!config.params
        },
    }

    onFulfilled(request: AxiosRequestConfig): AxiosRequestConfig | Promise<AxiosRequestConfig> {
        request.paramsSerializer = {
            serialize: (params) => stringify(params, { arrayFormat: 'repeat' }),
        } as ParamsSerializerOptions
        return request
    }

}
